package com.jinlejia.erp.ui.common;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.freedom.yefeng.yfrecyclerview.YfListRecyclerView;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.Source1Adapter;
import com.weight.CustomToast;

import java.util.ArrayList;
import java.util.Map;

/**
 * 委托来源
 */
public class SourceTypeDialog extends AlertDialog {
    private Context mContext;
    private YfListRecyclerView recyclerView1;
    private YfListRecyclerView recyclerView2;
    private ArrayList<Map<String, Object>> data;
    private Source1Adapter adapter;
    private Source1Adapter adapter2;
    private Map<String, Object> source1;
    private Map<String, Object> source2;

    public SourceTypeDialog(Context context, ArrayList<Map<String, Object>> list) {
        super(context);
        mContext = context;
        this.data = list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setCanceledOnTouchOutside(true);
        Window window = this.getWindow();
        // *** 主要就是在这里实现这种效果的.
        window.setContentView(R.layout.dialog_source_type);
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        window.setBackgroundDrawable(new ColorDrawable());

        recyclerView1 = window.findViewById(R.id.yf_recycler1);
        recyclerView1.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView1.setHasFixedSize(true);
        adapter = new Source1Adapter(data);
        recyclerView1.setAdapter(adapter);

        recyclerView2 = window.findViewById(R.id.yf_recycler2);
        recyclerView2.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView2.setHasFixedSize(true);
        adapter2 = new Source1Adapter(new ArrayList<>());
        recyclerView2.setAdapter(adapter2);

        adapter.setOnItemSelectedListener(map -> {
            source1 = map;
            ArrayList<Map<String, Object>> data2 = JSON.parseObject(map.get("dictionaryList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
            });
            adapter2.setData(data2);
        });
        adapter2.setOnItemSelectedListener(map -> source2 = map);

        window.findViewById(R.id.bt_confirm).setOnClickListener(v -> {
            if (onSelectedListener != null) {
                if (null == source1) {
                    CustomToast.showToast(getContext(), "请选择委托来源", 1500);
                } else if (null == source2) {
                    CustomToast.showToast(getContext(), "请选择二级委托来源", 1500);
                } else {
                    onSelectedListener.onSelected(source1, source2);
                }
            }
        });

    }

    public interface OnSelectedListener {
        void onSelected(Map<String, Object> source1, Map<String, Object> source2);
    }

    private OnSelectedListener onSelectedListener;

    public void setOnSelectedListener(OnSelectedListener listener) {
        this.onSelectedListener = listener;
    }

}
